<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<script type="text/javascript">
$(function(){ 
	var $t     = $('#department_tree');
	var op     = $t.data();
	//添加删除按钮INIT
	var add    = op.addHoverDom ? null : 'edit';
	var remove = op.removeHoverDom ? null : 'edit';
	var bR     = op.beforeRemove ? null : 'departmentBeforeRemove';
	var onremove = op.onRemove ? '' : 'departmentOnRemove';
	$t.data('addHoverDom', add).data('removeHoverDom', remove).data('beforeRemove', bR).data("onRemove",onremove);
	//拖动INIT
	var edit   = op.editEnable ? true : true,
        bDrop  = op.beforeDrop ? '' : 'departmentBeforeNodeDrop',
        onDrop = op.onDrop ? '' : 'departmentOnNodeDrop'; 
    $t.data('editEnable', edit).data('beforeDrop', bDrop).data('onDrop', onDrop);
});

function departmentTreeOnclick(event, treeId, treeNode, clickFlag){
	event.preventDefault();
	if("1" == treeNode.id){
		return;
	}
	var data = {"departmentNo":treeNode.departmentno, "departmentSn":treeNode.departmentsn};
	//console.log(data);
	//调用后台 初始化页面
	$("#department_detail").bjuiajax('doLoad', {
		"url"    : "department/getDepartment.do",
		"data"   : data,
		"target" : $("#department_detail")
	});
}

//添加部门之前的事件
function departmentBeforeAddChild(_this, event, treeId, treeNode) {
	$(document.body).showLoading();
	var departmentNo = new Date().getTime();
	var departmentName = "新增部门";
	var departmentSn = null;
	var url = "department/add.do";
	var data = {"departmentNo":departmentNo, "departmentName":departmentName, "parentDepartSn": treeNode.id};
	var successCallBack = function(result){
		departmentSn = result.data[0];
	}
	var failCallBack = function(result){
		$("#department_tree").alertmsg('error', result.message);
		return false;
	}
	var errorCallBack = function(xhr, ajaxOptions, thrownError) {
		$(document.body).bjuiajax('ajaxError', xhr, ajaxOptions, thrownError);
		return false;
	}
	myAjax(url, data, false, successCallBack, failCallBack, errorCallBack);
	var zTree = $.fn.zTree.getZTreeObj('department_tree');
	zTree.addNodes(treeNode, {id: departmentSn, name:departmentName, departmentsn: departmentSn, departmentno: departmentNo});
	return false;
}

//删除前事件
function departmentBeforeRemove(treeId, treeNode) {
	if("1" == treeNode.id){
		$("#department_tree").alertmsg('info', '部门根节点不能删除！');
    	return false;
	}
	
    if(treeNode.isParent){
    	$("#department_tree").alertmsg('info', '请先删除子部门！');
    	return false;
    }

    return true;
}

//删除结束事件
function departmentOnRemove(event, treeId, treeNode) {
	var url = "department/del.do?departmentSn="+treeNode.departmentsn+"&departmentNo="+treeNode.id;
	var data = null;
	var successCallBack = function(result){
		$("#department_tree").alertmsg('info', result.message);
	}
	var failCallBack = function(result){
		$("#department_tree").alertmsg('error', result.message);
		return false;
	}
	var errorCallBack = function(xhr, ajaxOptions, thrownError) {
		$(document.body).bjuiajax('ajaxError', xhr, ajaxOptions, thrownError);
		return false;
	}
	myAjax(url, data, true, successCallBack, failCallBack, errorCallBack);
}

//监听拖拽结束之前事件
function departmentBeforeNodeDrop(treeId, treeNodes, targetNode, moveType, isCopy) {
	//alert(moveType);
	if(!moveType) { // 如果 moveType = null，表明拖拽无效
		return false;
	}
	if(moveType != 'inner' && targetNode.id == '1'){
		return false;
	}
	var departmentSn = treeNodes[0].id;
	var parentDepartSn = null;
	if(moveType == 'inner'){ // "inner"：成为子节点
		parentDepartSn = targetNode.id;
	} else {
		parentDepartSn = targetNode.getParentNode().id;
	}
	$.ajax({
		url: "department/updatepid.do",
		async: false,
		data : {"departmentSn":departmentSn, "parentDepartSn": parentDepartSn},
		type: "post",
		dataType: "json",
		success: function(res){
			if("success"==res.resultCode){
				
			}else{
				$("#department_tree").alertmsg('error', res.message);
				return false;
			}
		},
		error: function(xhr, ajaxOptions, thrownError){
			$('#department_form').bjuiajax('ajaxError', xhr, ajaxOptions, thrownError);
			return false;
		}
    });
	
	return true
}

//监听拖拽结束事件
function departmentOnNodeDrop(event, treeId, treeNodes, targetNode, moveType, isCopy) {
	
}

//保存属性到菜单树
function syncDepartmentTree() {
	var zTree  = $.fn.zTree.getZTreeObj("department_tree");
	var upNode = zTree.getSelectedNodes()[0];
	if (!upNode) {
		$("#department_tree").alertmsg('error', '未选中任何部门！');
        return
	}
	
	var departmentName = $('#t_department_name').val();
	var departmentNo = $('#t_department_no').val();
	var departmentSn = $('#t_department_sn').val();
	upNode.departmentno=departmentNo;
	upNode.name=departmentName;
	upNode.departmentsn=departmentSn;

	zTree.updateNode(upNode);
}
</script>
<div class="bjui-pageContent" style="display: inline-block;">
	<div style="float: left; margin: 5px 5px 0; width: 29%; height: 99%;">
		<fieldset style="height: 99%;">
	        <legend>部门结构</legend>
	        
	        	<div style="float: left; width: 99%; height: 96%; overflow: auto;">
	        		<ul id="department_tree" class="ztree" style="width: 99%;" data-toggle="ztree"
				      	data-options='{maxAddLevel:5, beforeAddChild:"departmentBeforeAddChild", expandAll:true, onClick:"departmentTreeOnclick"}'>
						<li data-id="1" data-pId="">公司部门</li>
						<c:forEach items="${departments}" var="department">
						<li data-id="${department.departmentSn}" data-pId="${department.parentDepartSn}" 
							data-departmentsn="${department.departmentSn}" data-departmentno="${department.departmentNo}"
							>${department.departmentName}</li>
						</c:forEach>
					</ul>
	        	</div>
	        
	    </fieldset>
    </div>
    <div style="margin-left: 30%; margin-top: 5px; width: 69%; height: 99%;">
    	<fieldset style="height: 99%;" >
	    	<legend>部门详细信息</legend>
	    	<div id="department_detail" class="clearfix active in" style="height:100%; width:100%;">
	    		
	    	</div>
	    </fieldset>
	</div>
</div>